package com.example.administrator.mymusic.Class;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.mymusic.R;

import java.util.List;

/**
 * Created by Administrator on 2016/4/5.
 */
public class MySongsAdapter extends BaseAdapter {
    private Context context;
    private List<SongsClass> datas;
    private SongsClass songsClass;

    public MySongsAdapter(Context context, List<SongsClass> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public SongsClass getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_songs, null);
            viewHolder = new ViewHolder();
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.item_iv);
            viewHolder.name = (TextView) convertView.findViewById(R.id.item_name);
            viewHolder.artist = (TextView) convertView.findViewById(R.id.item_artist);
            viewHolder.duration = (TextView) convertView.findViewById(R.id.item_duration);
            convertView.setTag(viewHolder);            //表示给View添加一个格外的数据，
        } else {
            viewHolder = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来
        }
        songsClass = datas.get(position);

        viewHolder.iv.setImageResource(R.mipmap.log);
        viewHolder.name.setText(songsClass.getName());            //显示标题
        viewHolder.artist.setText("" + songsClass.getArtist());        //显示艺术家
        viewHolder.duration.setText(formatTime(songsClass.getDuration()));//显示时长
        convertView.setAnimation(Alpha());
        return convertView;
    }


    class ViewHolder {
        public ImageView iv;    //专辑图片
        public TextView name;        //音乐标题
        public TextView duration;    //音乐时长
        public TextView artist;    //音乐艺术家

    }

    public AlphaAnimation Alpha() {
        AlphaAnimation animation = new AlphaAnimation(0, 1);
        animation.setDuration(1000);
        return animation;
    }

    public static String formatTime(long time) {
        String min = time / (1000 * 60) + "";
        String sec = (time % (1000 * 60)) / 1000 + "";
        if (min.length() < 2) {
            min = "0" + time / (1000 * 60) + "";
        } else {
            min = time / (1000 * 60) + "";
        }
        if (sec.length() == 1) {
            sec = "0" + sec;
        }
        return min + ":" + sec;
    }


    private String getAlbumArt(int album_id) {
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[]{"album_art"};

        Cursor cur = context.getContentResolver().query(
                Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
                projection, null, null, null);
        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        cur = null;
        return album_art;
    }


//    private void getAmbum(Context context, long songid, long albumid) {
//        Bitmap bm = null;
//
//        if (albumid < 0) {
//            Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
//            ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
//            if (pfd != null) {
//                FileDescriptor fd = pfd.getFileDescriptor();
//                bm = BitmapFactory.decodeFileDescriptor(fd);
//            }
//            //如果有专辑图片id，则直接去取图片
//            else {
//                uri = ContentUris.withAppendedId(uri.parse("content://media/external/audio/albumart"),
//                        albumid);
//                pfd = getContentResolver().openFileDescriptor(uri, "r");
//                if (pfd != null) {
//                    FileDescriptor fd = pfd.getFileDescriptor();
//                    bm = BitmapFactory.decodeFileDescriptor(fd);
//                }
//            }
//        }
//


}
